package com.weimei.conversionTreasure.weight;

import android.content.Context;
import android.util.Log;
import android.widget.DatePicker;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.core.CenterPopupView;
import com.weimei.conversionTreasure.R;
import com.weimei.conversionTreasure.utils.ValidateUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.ToastUtils;

public class BottomRulePop extends CenterPopupView {

    private Click mClick;
    private TextView sure, cancel, tv_ls;
    private RecyclerView timeRecy;
    private Context mContext;

    int selLun = 0;

    RuleTimeAdapter adapter;


    public interface Click {
        void click(String s,List<RuleBean> list);
    }

    public BottomRulePop(@NonNull Context context, Click click) {
        super(context);
        mContext = context;
        this.mClick = click;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.layout_bottom_rule;
    }

    @Override
    protected void onCreate() {
        cancel = findViewById(R.id.cancel);
        sure = findViewById(R.id.sure);
        timeRecy = findViewById(R.id.time_recy);
        tv_ls = findViewById(R.id.tv_ls);

        cancel.setOnClickListener(view -> dismiss());
        sure.setOnClickListener(view -> {
            if (selLun != 0){
                for (int i = 0; i < selLun; i++) {
                    if (ValidateUtils.isEmptyString(adapter.getData().get(i).selTime)){
                        ToastUtils.showShort("请选择提醒时间");
                        return;
                    }
                }
                mClick.click(selLun+"轮", adapter.getData());
                dismiss();

            }else {
                ToastUtils.showShort("请选择提醒轮数");
            }

        });

        tv_ls.setOnClickListener(view -> {
            List<String> list = new ArrayList<>();
            list.add("1轮");
            list.add("2轮");
            list.add("3轮");
            list.add("4轮");
            list.add("5轮");

            new XPopup.Builder(mContext)
                    .moveUpToKeyboard(false)//如果不加这个，评论弹窗会移动到软键盘上面
                    .enableDrag(true)
                    .asCustom(new BottomWheelPop(mContext, list, s1 -> {
                        selLun = Integer.parseInt(s1.replace("轮", ""));
                        tv_ls.setText(s1);

                        adapter = new RuleTimeAdapter();
                        timeRecy.setAdapter(adapter);
                        List<RuleBean> list1 = new ArrayList<>();
                        for (int i = 0; i < selLun; i++) {
                            RuleBean ruleBean = new RuleBean();
                            ruleBean.name = i + 1 + "轮";
                            list1.add(ruleBean);
                        }
                        adapter.setList(list1);



                    }))
                    .show();
        });


    }

    public class RuleTimeAdapter extends BaseQuickAdapter<RuleBean, BaseViewHolder> {

        public RuleTimeAdapter() {
            super(R.layout.item_rule_time);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder holder, RuleBean s) {
            holder.setText(R.id.name, s.name + "提醒");

            TextView tv = holder.getView(R.id.tv_time);
            tv.setOnClickListener(v1 -> {
                new XPopup.Builder(mContext)
                        .moveUpToKeyboard(false)//如果不加这个，评论弹窗会移动到软键盘上面
                        .enableDrag(true)
                        .asCustom(new BottomTimePop(mContext, s1 -> {
                            tv.setText(s1);
                            s.selTime = s1;
                        },true,true))
                        .show();
            });

        }
    }


    public class RuleBean{
        public String name;
        public String selTime;

        @Override
        public String toString() {
            return "RuleBean{" +
                    "name='" + name + '\'' +
                    ", selTime='" + selTime + '\'' +
                    '}';
        }
    }


}
